package com.yanqu.road.server.manager.tomb;

import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.manager.tomb.astar.Coordinate;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

public class Position {
    public int row;
    public int column;

    public int pEnum;

    public String pRelation = "";

    public int flag;

    public int eType;

    public String decoration = "";

    public long killUserId = 0;

    public int lockIndex = 0;
    private long timestamp;

    private String key;

    public Position(int row, int column) {
        this.row = row;
        this.column = column;
        this.key = String.format("%d_%d", row, column);
        this.timestamp = Instant.now().toEpochMilli();
    }


    public Position(String s) {
        if (s == null) {
            return;
        }
        String[] split = s.split("_");
        if (split.length != 2) {
            return;
        }
        this.row = Integer.parseInt(split[0]);
        this.column = Integer.parseInt(split[1]);
        this.key = String.format("%d_%d", row, column);

    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public int getpEnum() {
        return pEnum;
    }

    public void setpEnum(int pEnum) {
        this.pEnum = pEnum;
    }

    public String getpRelation() {
        return pRelation;
    }

    public void setpRelation(String pRelation) {
        this.pRelation = pRelation;
    }


    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("p=");
        stringBuilder.append(this.pEnum);

        stringBuilder.append(";x=");
        stringBuilder.append(this.row);
        stringBuilder.append(";y=");
        stringBuilder.append(this.column);
        if (this.pRelation.length() > 0) {
            stringBuilder.append(";");
            stringBuilder.append("pr=");
            stringBuilder.append(this.pRelation);
        }

        if (this.flag == 1) {
            stringBuilder.append(";");
            stringBuilder.append("f=1");
        }
        if (this.eType > 0) {
            stringBuilder.append(";");
            stringBuilder.append("et=");
            stringBuilder.append(this.eType);
        }

        if (this.decoration.length() > 0) {
            stringBuilder.append(";");
            stringBuilder.append("d=");
            stringBuilder.append(this.decoration);
        }
        if (this.killUserId > 0) {
            stringBuilder.append(";");
            stringBuilder.append("k=");
            stringBuilder.append(this.killUserId);
        }
        stringBuilder.append(";");
        stringBuilder.append("l=");
        stringBuilder.append(this.lockIndex);
        return stringBuilder.toString();
    }

    public void parseString(String s) {
        if (s == null) {
            return;
        }
        String[] split = s.split(";");
        for (String s1 : split) {
            String[] split1 = s1.split("=");
            if (split1.length != 2) {
                continue;
            }
            switch (split1[0]) {
                case "p":
                    this.pEnum = Integer.parseInt(split1[1]);
                    break;

                case "pr":
                    this.pRelation = split1[1];
                    break;
                case "f":
                    int i = Integer.parseInt(split1[1]);
                    if (i == 1) {
                        this.flag = 1;
                    }
                    break;
                case "et":
                    this.eType = Integer.parseInt(split1[1]);
                    break;
                case "d":
                    this.decoration = split1[1];
                    break;
                case "l":
                    this.lockIndex = Integer.parseInt(split1[1]);
                    break;

            }
        }
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public void refreshTimestamp() {
        this.timestamp = Instant.now().toEpochMilli();
    }

    public int geteType() {
        return eType;
    }

    public void seteType(int eType) {
        this.eType = eType;
    }

    public String getDecoration() {
        return decoration;
    }

    public void setDecoration(String decoration) {
        this.decoration = decoration;
    }


    public long getKillUserId() {
        return killUserId;
    }

    public void setKillUserId(long killUserId) {
        this.killUserId = killUserId;
    }


    public int getLockIndex() {
        return lockIndex;
    }

    public void setLockIndex(int lockIndex) {
        this.lockIndex = lockIndex;
    }


    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public boolean canThrough() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_ROAD_VALUE || this.pEnum == Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE) {
            return true;
        }
        if (this.eType == Tomb.EventEnum.EVENT_BUFF_VALUE ||
                this.eType == Tomb.EventEnum.EVENT_TRAP_VALUE ||
                this.eType == Tomb.EventEnum.EVENT_ENTER_VALUE ||
                this.eType == Tomb.EventEnum.EVENT_PVP_BATTLE_VALUE ||
                this.eType == Tomb.EventEnum.EVENT_CHOOSE_VALUE) {
            return true;
        }

        return false;
    }

    public boolean canExploreThrough() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_NONE_VALUE
                || this.pEnum == Tomb.PositionEnum.POSITION_WALL_VALUE
                || this.pEnum == Tomb.PositionEnum.POSITION_DECORATION_VALUE) {
            return false;
        }
        return true;
    }

    public boolean canStand() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_ROAD_VALUE ||
                this.pEnum == Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE) {
            return true;
        }
        return false;
    }


    public boolean isDone() {
        return this.pEnum == Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE;
    }

    public boolean hasEvent() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_EVENT_VALUE) {
            return true;
        }
        return false;
    }

    public boolean canExplore() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_EXPLORE_VALUE) {
            return true;
        }
        return false;
    }

    public boolean isBuffEvent() {
        return this.geteType() == Tomb.EventEnum.EVENT_BUFF_VALUE;
    }

    public boolean canClick() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_EVENT_VALUE ||
                this.pEnum == Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE ||
                this.pEnum == Tomb.PositionEnum.POSITION_ROAD_VALUE ||
                this.pEnum == Tomb.PositionEnum.POSITION_EXPLORE_VALUE) {
            return true;
        }
        return false;
    }


    public boolean UnionExplore() {
        if (this.pEnum == Tomb.PositionEnum.POSITION_EVENT_VALUE) {
            if (this.eType == Tomb.EventEnum.EVENT_NPC_BATTLE_VALUE ||
                    this.eType == Tomb.EventEnum.EVENT_ELITE_NPC_BATTLE_VALUE ||
                    this.eType == Tomb.EventEnum.EVENT_BOSS_BATTLE_VALUE ||
                    this.eType == Tomb.EventEnum.EVENT_ENTER_VALUE) {
                return true;
            }
        }
        return false;
    }

}
